<!-- eslint-disable vue/multi-word-component-names -->
// eslint-disable-next-line vue/multi-word-component-names
<template>
  <div id="building">
    <div class="box">
      <el-form
        ref="login"
        :model="user"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <p class="p1">默然后台管理系统</p>
        <el-form-item label="用户名" prop="userName">
          <el-input class="in1" v-model="user.userName" autocomplete="off" placeholder="用户名必须6-12位数字字母汉字组成"/>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input
            class="in1"
            placeholder="字母开头的4-8位数字字母下划线组成"
            v-model="user.pwd"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="userLogin">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { userLogin } from '../../api/user'
export default {
  name: 'LoginPage',
  data () {
    return {
      user: {
        userName: '',
        pwd: ''
      },
      rules: {
        userName: [
          {
            required: true,
            message: '用户名必填'
          },
          {
            pattern: /^[\u4e00-\u9fa5a-zA-Z0-9]{6,12}$/,
            message: '用户名必须6-12位数字字母汉字组成'
          }
        ],
        pwd: [
          {
            required: true,
            message: '密码必填'
          },
          {
            pattern: /^[a-zA-Z]\w{3,7}$/,
            message: '字母开头的4-8位数字字母下划线组成'
          }
        ]
      }
    }
  },
  methods: {
    userLogin () {
      this.$refs.login.validate().then(() => {
        userLogin(this.user).then(res => {
          if (res.data.code === 200) {
            const { token, role, userInfo } = res.data.data
            localStorage.setItem('token', token)
            localStorage.setItem('role', role)
            localStorage.setItem('userInfo', JSON.stringify(userInfo))
            this.$store.commit('user/INIT_USER', res.data.data)
            this.$message({
              type: 'success',
              message: res.data.msg,
              duration: 2000,
              onClose: () => {
                this.$router.replace('/dashBoard')
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '验证失败'
        })
      })
    },
    resetForm () {
      this.$refs.login.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
#building{
  background:url("../../../public/pic2.webp");
  width:100%;
  height:100%;
  position:fixed;
  background-size:100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.box{
  width: 400px;
  height: 400px;
  background-color: #f5f5f5;
  border-radius: 30px;
}
.p1{
  font-size: 30px;
  color: gray;
  text-align: center;
  margin: 30px auto;
}
.in1{
  width: 260px;
}
</style>
